package cn.tedu.system.admin.service.impl;

import cn.tedu.system.admin.mapper.AdminMapper;
import cn.tedu.system.admin.pojo.dto.AdminAddUserParam;
import cn.tedu.system.admin.pojo.dto.UpdateUserStatusDTO;
import cn.tedu.system.admin.pojo.dto.UserAddDto;
import cn.tedu.system.admin.pojo.dto.UserSaveParam;
import cn.tedu.system.admin.pojo.vo.AllUserVO;
import cn.tedu.system.base.exception.ServiceException;
import cn.tedu.system.base.response.StatusCode;
import cn.tedu.system.merchant.mapper.MerchantMapper;
import cn.tedu.system.merchant.pojo.dto.MerchantCheckDto;
import cn.tedu.system.merchant.pojo.vo.MerchantStatusVO;
import cn.tedu.system.admin.service.AdminService;
import cn.tedu.system.user.pojo.entity.User;
import cn.tedu.system.user.pojo.vo.UserVO;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import javax.servlet.http.HttpSession;
import java.util.Date;
import java.util.List;

@Slf4j
@Service
@Transactional
public class AdminServiceImpl implements AdminService {
    @Autowired
    private AdminMapper adminMapper;
    @Autowired
    private MerchantMapper merchantMapper;

    //查看所有用户
    @Override
    public List<AllUserVO> selAllUser() {
        log.debug("查看所有用户业务");
        List<AllUserVO> allUserVO = adminMapper.selAllUser();
        if (allUserVO == null && allUserVO.size() == 0) {
            throw new ServiceException(StatusCode.USER_NOT_EXIST_OR_ERROR);
        }
        return allUserVO;
    }

    //修改用户信息
    @Override
    public void makeStatus(UserSaveParam userSaveParam,HttpSession session) {
        log.debug("修改用户信息业务:{}", userSaveParam);
        UserVO userVO = (UserVO) session.getAttribute("user");
        if (userVO == null) {
            throw new ServiceException(StatusCode.UNAUTHORIZED);
        }
        if (userVO.getRole()!=3){
            throw new ServiceException(StatusCode.FORBIDDEN);
        }

        User user = new User();
        BeanUtils.copyProperties(userSaveParam,user);
        if (userSaveParam.getId()==null){
            user.setCreateTime(new Date());
            user.setUpdateTime(new Date());
            if(adminMapper.insertUser(user)<=0){
                throw new ServiceException(StatusCode.FAILED);
            }
        }else{
            user.setUpdateTime(new Date());
            if(adminMapper.makeStatus(user)<=0){
                throw new ServiceException(StatusCode.FAILED);
            }
        }
    }

    //查看需审核的商家
    @Override
    public List<MerchantStatusVO> selCheckStore(HttpSession session) {
        log.debug("管理员查看审核商家店铺");
        UserVO userVO = (UserVO) session.getAttribute("user");
        if (userVO == null) {
            throw new ServiceException(StatusCode.UNAUTHORIZED);
        }
        if (userVO.getRole()!=3){
            throw new ServiceException(StatusCode.FORBIDDEN);
        }

        List<MerchantStatusVO> merchantStatusVO = merchantMapper.selCheckStore();
        if (merchantStatusVO == null && merchantStatusVO.size() == 0) {
            throw new ServiceException(StatusCode.FAILED);
        }
        return merchantStatusVO;
    }

    //审核店铺功能
    @Override
    public void checkStore(MerchantCheckDto merchantCheckDto,HttpSession session) {
        log.debug("审核店铺业务:{}", merchantCheckDto);
        UserVO userVO = (UserVO) session.getAttribute("user");
        if (userVO == null) {
            throw new ServiceException(StatusCode.UNAUTHORIZED);
        }
        if (userVO.getRole()!=3){
            throw new ServiceException(StatusCode.FORBIDDEN);
        }

        if(merchantMapper.checkStore(merchantCheckDto)<=0){
            throw new ServiceException(StatusCode.FAILED);
        }
    }
    //所有店铺
    @Override
    public List<MerchantStatusVO> selAllStore(HttpSession session) {
        log.debug("查看所有店铺业务层");
        UserVO userVO = (UserVO) session.getAttribute("user");
        if (userVO == null) {
            throw new ServiceException(StatusCode.UNAUTHORIZED);
        }
        if (userVO.getRole()!=3){
            throw new ServiceException(StatusCode.FORBIDDEN);
        }

        List<MerchantStatusVO> list = adminMapper.selectAllStore();
        return list;
    }


    //更新用户状态--农睿昌
    @Override
    public void reviseUserStatus(UpdateUserStatusDTO updateUserStatusDTO) {
        log.debug("修改用户状态业务:{}", updateUserStatusDTO);

        // 这里可以添加额外的逻辑，例如检查用户权限等
        int result = adminMapper.updateUserStatus(updateUserStatusDTO);

        if (result <= 0) {
            throw new ServiceException(StatusCode.FAILED);
        }
    }

    //添加用户
    @Override
    public void addUser(AdminAddUserParam userAdd, HttpSession session) {
        log.debug("管理员添加用户业务:{}", userAdd);
        UserVO userVO = (UserVO) session.getAttribute("user");
        if (userVO == null) {
            throw new ServiceException(StatusCode.UNAUTHORIZED);
        }
        if (userVO.getRole()!=3){
            throw new ServiceException(StatusCode.FORBIDDEN);
        }
        User user = new User();
        BeanUtils.copyProperties(userAdd,user);
        user.setCreateTime(new Date());

        if(adminMapper.insertUser(user)<=0){
            throw new ServiceException(StatusCode.FAILED);
        }
    }

    //删除用户
    @Override
    public void delUser(Long userId, HttpSession session) {
        log.debug("管理员删除用户业务:{}", userId);
        UserVO userVO = (UserVO) session.getAttribute("user");
        if (userVO == null) {
            throw new ServiceException(StatusCode.UNAUTHORIZED);
        }
        if (userVO.getRole()!=3){
            throw new ServiceException(StatusCode.FORBIDDEN);
        }

        if(adminMapper.deleteUserById(userId)<=0){
            throw new ServiceException(StatusCode.FAILED);
        }
    }
}
